package u1;

import static java.lang.System.*;
import static u1.Util.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * As a program, reads adjacency list on stdin and outputs histogram of links
 * per page on stdout. Optionally takes one argument: the name of a file of node
 * numbers to filter on; only links originating at a node in this list will be
 * counted.
 */
public class LinkHistogram {
  public static void main(String[] args) throws FileNotFoundException {
    Set<Integer> filter = null;
    if (args.length > 0) {
      List<Integer> filterList = new ArrayList<Integer>();
      readIntegers(filterList, new FileInputStream(new File(args[0])));
      filter = new HashSet<Integer>();
      filter.addAll(filterList);
    }
    out.println(new LinkHistogram(in, filter).getHistogram());
  }

  private Histogram hist;

  /**
   * @param is
   *                not null
   * @param filter
   *                may be null; if null, filter is ignored.
   */
  public LinkHistogram(InputStream is, final Set<Integer> filter) {
    hist = new Histogram();

    AdjReader reader = new AdjReader() {

      @Override
      public void readLinks(Integer source, Collection<Integer> targets) {
        if (filter != null && !filter.contains(source))
          return;
        
        hist.record(targets.size());

        // This takes a while; give some output.
        if ((getPageIndex() + 1) % 100000 == 0)
          trace("Counted %d pages.", getPageIndex() + 1);
      }

    };
    reader.read(is);
    trace("Counted %d pages.", reader.getPageIndex());
    trace("Counted %d links.", reader.getLinkCount());
  }

  public Histogram getHistogram() {
    return hist;
  }
}
/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

